$(function() {
$("#searchid").linkbutton({
	 text:'查询',
	 iconCls:'icon-search'
});
var positionStr = '您所在的位置：管理员后台&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;商家/品牌管理&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;商家发布信息管理';
	if(type == '2')
		positionStr = '您所在的位置：商家后台管理&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;信息管理';
	$('#shopermessgerlist').datagrid({
		title : positionStr,
		idField : 'id',
		nowrap : false,
		striped : true,
		collapsible : false,
		width : 1000,
		height : 450,
		url : '../shoperMessage/adminsearch.shtml',
		queryParams:{'brandid':$("#brandid").val()},
		frozenColumns : [[{
					field : 'ck',
					title : '选择',
					checkbox : true
				}]],
		columns : [[{
					title : '基本信息',
					colspan : 7
				}, {
					field : 'opt',
					title : '操作',
					width : 160,
					align : 'center',
					rowspan : 2,
					formatter : function(value, rec) {
						if (rec.id == undefined)
							return null;
						else{
									if(type==2){
									    	//  if( rec.autding==0 ||  rec.autding==2){
											return '<span style="color:red"><span onclick=Onmodify("'
													+ rec.id
													+ '",0)>&nbsp;<font style="cursor:pointer">修改</span></font> <span onclick=Ondelete("'
													+ rec.id + '")><font style="cursor:pointer">删除</font></span></span>';
										//	}else{
											//  return '<span onclick=Ondelete("'
												//	+ rec.id + '")><font style="cursor:pointer" color="red">删除</font></span></span>';
											//}
									}
									else
									{  var recommend = rec['recommend'];
									if(recommend == '3'||recommend == '4'||recommend == '5'){
									 return '<span style="color:red"><span onclick=look("'
									+ rec.id
									+ '",1)><font style="cursor:pointer">查看</font></span>&nbsp;&nbsp;<span onclick=Onmodify("'
									+ rec.id
									+ '",0)><font style="cursor:pointer">修改</font></span>&nbsp;&nbsp;<span onclick=Ondelete("'
									+ rec.id + '")><font style="cursor:pointer">删除</font></span>&nbsp;&nbsp;<span onclick=canceltuijian("'
									+ rec.id
									+ '")><font style="cursor:pointer;color:blue">取消推荐</font></span>&nbsp;&nbsp;&nbsp;<span onclick=showtuijian("'
									+ rec.id
									+ '")><font style="cursor:pointer;color:blue">推荐详情</font></span><hr><select  id=aud_'+rec.id +'><option value="1">审核通过</option><option value="2">审核不通过</option></select>&nbsp;<span onclick=OnAutding("'
									+ rec.id + '")><font style="cursor:pointer">审核</font></span></span>';
										
									}else{
										 return '<span style="color:red"><span onclick=look("'
									+ rec.id
									+ '",1)><font style="cursor:pointer">查看</font></span>&nbsp;&nbsp;<span onclick=Onmodify("'
									+ rec.id
									+ '",0)><font style="cursor:pointer">修改</font></span>&nbsp;&nbsp;<span onclick=Ondelete("'
									+ rec.id + '")><font style="cursor:pointer">删除</font></span>&nbsp;&nbsp;<span onclick=tuijian("'
									+ rec.id
									+ '")><font style="cursor:pointer">首页推荐</font></span></span><hr><select  id=aud_'+rec.id +'><option value="1">审核通过</option><option value="2">审核不通过</option></select>&nbsp;<span onclick=OnAutding("'
									+ rec.id + '")><font style="cursor:pointer">审核</font></span></span>';
									}
									     
									 }
							 }
					}
				}], [{
					field : 'title',
					title : '标题',
					width : 200
				}
				,{
					field : 'descrt',
					title : '商家/品牌名称',
					width : 120
				},{
					field : 'creatTimeStr',
					title : '创建时间',
					mapping : 'creatTimeStr',
					align : 'center',
					width : 140
				},{
					field : 'recommend',
					title : '首页推荐',
					align : 'center',
					mapping : 'creatTimeStr',
					width : 80,
					formatter:function(value,rec){
						if(value == '3'){
							return "<font color='green'>已推荐</font><br>(热门活动)";							
						}if(value == '4'){
							return "<font color='green'>已推荐</font><br>(全站热点)";							
						}if(value == '5'){
							return "<font color='green'>已推荐</font><br>(资讯)";							
						}
						if(value == '2'|| value==null){
							return "<font color='green'>未推荐</font>";							
						}	
					}
				} ,{
					field : 'isTop',
					title : '是否置顶',
					width : 60,
					formatter : function(value, rec) {
						   if(value=='1')return '<font color="red">是</font>';
						   else return '否';
					},
					mapping : 'authorName',
					align : 'center' 
				},{
					field : 'deleteFlag',
					title : '删除状态',
					align : 'center',
					width : 70,
					formatter : function(value, rec) {
					     if(value==1)return "<font color='red'>已删除</font>";
						     return  "未删除";
						 
					}
				},{
					field : 'autding',
					title : '审核状态',
					align : 'center',
					width : 90,
					formatter : function(value, rec) {
					     if(value==0) {
						     return  "未审核";
					     }
					     if(value==1)return "<font color='green'>审核通过</font>";
					     if(value==2)return "<font color='red'>审核未通过</font>";
						 
					}
				}

		]],
		pagination : true,
		rownumbers : true,
		toolbar : [  {
					id : 'btnadd',
					text : '添加信息',
					iconCls : 'icon-add',
					handler : function() {
					    var brandid = $("#brandid").val();
						$(this).attr("href", '../shoperMessage/toAddMessage.shtml?brandid='+brandid);
						
					 }
				},'-',{
					id : 'btnsave',
					text : '删除信息',
					iconCls : 'icon-remove',
					handler : function() {
						var records = $('#shopermessgerlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要删除的记录');
							return;
						}
						if (confirm("确定删除此条信息吗?")) {
						for (var i = 0; i < records.length; i++) {
							if (records[i].id == undefined
									|| records[i].id == "undefined") {

								alert('此条记录已不存在');
								return;
							}
							ids += records[i].id;
							ids += '_';
						}
					    deletebyId(ids)
					}
					}
				},'-',{
					id : 'btnadd',
					text : '置顶',
					iconCls : 'icon-ok',
					handler : function() {
						var records = $('#shopermessgerlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要置顶的记录');
							return;
						}
						for (var i = 0; i < records.length; i++) {
							if (records[i].id == undefined
									|| records[i].id == "undefined") {
								alert('此条记录已不存在');
								return;
							}
							ids += records[i].id;
							ids += '_';
						}
						if (confirm("确定置顶所选记录吗?")) {
						var url1 = "../shoperMessage/changeIsTop.shtml";
						$.ajax({
									url : url1,
									data : "idString=" + ids + "&isTop=1",
									success : function(msg) {
										var obj = eval('(' + msg + ')');
										var result = obj[0]["message"];
										alert(result);
										$('#shopermessgerlist').datagrid("reload");
									}
								});
						}
					}
				},'-',{
					id : 'btnadd',
					text : '撤销置顶',
					iconCls : 'icon-undo',
					handler : function() {
						var records = $('#shopermessgerlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要撤销置顶的记录');
							return;
						}
						for (var i = 0; i < records.length; i++) {
							if (records[i].id == undefined
									|| records[i].id == "undefined") {
								alert('此条记录已不存在');
								return;
							}
							ids += records[i].id;
							ids += '_';
						}
						if (confirm("确定撤销置顶所选记录吗?")) {
						var url1 = "../shoperMessage/changeIsTop.shtml";
						$.ajax({
									url : url1,
									data : "idString=" + ids + "&isTop=2",
									success : function(msg) {
										var obj = eval('(' + msg + ')');
										var result = obj[0]["message"];
										alert(result);
										$('#shopermessgerlist').datagrid("reload");
									}
								});
						}
					}
				},'-',{
					id : 'btnback',
					text : '返回上一层',
					iconCls : 'icon-back',
					handler : function() {
						$(this).attr("href", '../shoper/branlist.jsp');
					}
				}]});
				
				
	$("#modifyWindow").dialog({
		title:'首页推荐设置',
		width:500,
		height:300,
		resizable:false,
		minimizable:false,
		maximizable:false,
		zIndex:100,
		closed:true
	})
	
	if(type=="2"){
		$('#btnback').hide();
	}
});

   function   onSearch(){
            var queryParams =$('#shopermessgerlist').datagrid("options").queryParams;
			queryParams.title=$("#title").val();
			queryParams.auding=$("#auding").val();
			$('#shopermessgerlist').datagrid("reload");
   }


function Ondelete(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定删除此条信息吗?")) {

		deletebyId(id);
	}
}
function   deletebyId(id)
{
  	var url1 = "../shoperMessage/deleteById.shtml";

						$.ajax({
									url : url1,
									data : "ids=" + id ,
									success : function(msg) {

										var obj = eval('(' + msg + ')');
										var result = obj["result"];
										if(result){alert('删除成功')}
										else{
										  alert('删除失败')
										}
										$('#shopermessgerlist').datagrid("reload");

									}
								});
						
}

function Onmodify(id,flag) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if(type=="2"){
	if (confirm("修改后需要重新核实,确定修改吗?")) {
		if(flag==1){
			window.location.href = "../shoperMessage/findMessageById.shtml?id=" + id+"&see=look" ;
		}else{
		    window.location.href = "../shoperMessage/findMessageById.shtml?id=" + id ;
		}
	}
	}else{
	   if(flag==1){
			window.location.href = "../shoperMessage/findMessageById.shtml?id=" + id+"&see=look" ;
		}else{
		    window.location.href = "../shoperMessage/findMessageById.shtml?id=" + id ;
		}
	}
}
function look(id,flag) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	
		if(flag==1){
			window.location.href = "../shoperMessage/findMessageById.shtml?id=" + id+"&see=look" ;
		}else{
		    window.location.href = "../shoperMessage/findMessageById.shtml?id=" + id ;
		}
}

function OnAutding(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定审核此条信息吗?")) {
        var autding=document.getElementById('aud_'+id).value;
		var url1 = "../shoperMessage/updateMessageById.shtml";

						$.ajax({
									url : url1,
									data : "id=" + id+"&autding="+autding ,
									cache:false,
									success : function(msg) {

										var obj = eval('(' + msg + ')');
				                       var result = obj["result"];
			                          if(!result){
                                            alert("审核失败");
                                           return;
						                     }
						                  alert("审核成功");
										$('#shopermessgerlist').datagrid("reload");
									}
								});
	}
}


function canceltuijian(id) {
	var url1 = "../shoperMessage/canceltuijian.shtml";
						$.ajax({
									url : url1,
									data : "id=" + id ,
									success : function(msg) {
										var obj = eval('(' + msg + ')');
										var result = obj["result"];
										if(result){alert('取消成功')}
										else{
										  alert('删除失败')
										}
										$('#shopermessgerlist').datagrid("reload");
									}
								});
}



function tuijian(id){
	$("#modifyWindow").window('open');
	$("#mesid").val(id);
	$.get('../category/category_loadCategory.shtml',function(mes){
			var json = eval('('+mes+')');
			var str = '<select id="bigCategory" name="tgInformation.PcategoryId" onChange="bigCategoryChange()"><option value="">--请选择--</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}	
			str+='</select>';
			$('#bigCategory').html(str);
	})
}

function showtuijian(id){
	 window.location.href = "../information/information_totuijianInfo.shtml?flag=2&id="+id;
}



function bigCategoryChange(){
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if(bigCategoryValue != ""){
		$('#smallCategory').html('加载中... ...');
		$('#goodsType').html('');
		$.get('../category/category_loadCategoryByPid.shtml?id='+bigCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="tgInformation.categoryId" id="categoryId" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#smallCategory').html(str);
		})
	}
}




function showfilediv() {
	if($('#imgurl').val()!=""){
	   $('#imgdiv').show();
	}
}


function showimg(url){
	 $('#imgdiv').show();
	 $('#img').attr('src',url);
	 $('#imgurl').val(url);
}

function showwhich(obj){
	  if(obj.value==3){
	     $('#trimgdiv').show();
	     $('#information').hide();
	  }
	  if(obj.value==1){
	      $('#information').show();
	       $('#hot2').show();
	       $('#hot1').show();
	  }
	  if(obj.value==2){
	    $('#information').show();
	    $('#hot2').hide();
	    $('#hot1').hide();
	  }
}

function recommend(){
   alert("推荐成功！");
   $('#the2').val("");
   $('#the1').val("");
    $('#mesid').val("");
   $("#modifyWindow").window('close');
   $('#shopermessgerlist').datagrid("reload");
   
}

function showimgdiv(obj){
	   if(obj.value==2){
	     $('#trimgdiv').hide();
	   }else{
	     $('#trimgdiv').show();
	   }
}  



